package com.fms.controllers.form3;

import home.HomePage;
import includes.Sessions;
import classes.Form3_2;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;

public class CashVoucher extends WebPage{
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	
	private Model rcaFormNoModel = new Model();
	private Model cvNoModel = new Model();
	private Model amountDisbursedModel = new Model();
	private Model adviserModel = new Model();
	private Model treasurerModel = new Model ();
	private Model amountWithdrawnFromModel = new Model();	
	
	public CashVoucher () throws IOException, SQLException{
		
		TextField rcaFormNo = new TextField ("rcaFormNo", rcaFormNoModel);
		rcaFormNo.setRequired(true);
		TextField cvNo = new TextField ("cvNo", cvNoModel);
		cvNo.setRequired(true);
		TextField amountDisbursed = new TextField ("amountDisbursed", amountDisbursedModel);
		amountDisbursed.setRequired(true);
		TextField adviser = new TextField ("adviser", adviserModel);
		adviser.setRequired(true);
		TextField treasurer = new TextField ("treasurer", treasurerModel);
		treasurer.setRequired(true);
		TextField amountWithdrawnFrom = new TextField ("amountWithdrawnFrom", amountWithdrawnFromModel);
		amountWithdrawnFrom.setRequired(true);
		
		Form form = new Form("cashVoucherForm");
		form.add(new Button("add"){
			private static final long serialVersionUID = 1L;
			public void onSubmit(){
				try{
					String rcaFormNo = (String) rcaFormNoModel.getObject();
					String cvNo = (String) cvNoModel.getObject();
					String amountDisbursed = (String) amountDisbursedModel.getObject();
					String adviser = (String) adviserModel.getObject();
					String treasurer = (String) treasurerModel.getObject();
					String amountWithdrawnFrom = (String) amountWithdrawnFromModel.getObject();
					
					Form3_2 form3_2 = new Form3_2();
					int newRcaFormNo = Integer.parseInt(rcaFormNo);
					form3_2.setRcaFormNo(newRcaFormNo);
					int newCvNo = Integer.parseInt(cvNo);
					form3_2.setCvNo(newCvNo);
					float newAmountDisbursed = Float.valueOf(amountDisbursed);
					form3_2.setAmountDisbursed(newAmountDisbursed);
					form3_2.setAdviser(adviser);
					form3_2.setTreasurer(treasurer);
					form3_2.setAmountWithdrawnFrom(amountWithdrawnFrom);
					sqlMap.insert("Form3_2",form3_2); //XML file??
					
					
					
					
					}catch (SQLException e){
					 error("Fill up necessary fields");
				//} catch (IOException e){
				//e.printStackTrace();
				}
			}
		});
	}

}
